package Function;

import FCRendering.FCRMaster;
import VWorldGameStudio.FCTank;
import utils.FCMixUtil;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

public class FCMutilPlayTankCache {
    /**
     * 缓存多人游戏中的坦克,
     * 本地的坦克不在这里
     */
    public static CopyOnWriteArrayList<FCTank> mutilTanks = new CopyOnWriteArrayList<>();

    public static FCTank findTankByName(String hostUid, String name) {
        for (FCTank tank : mutilTanks) {
            if (name.equals(tank.getName()) && tank.getHostId().equals(hostUid)) {
                return tank;
            }
        }
        return null;
    }



    /**
     * 表示除了本体的以外所有的角色都放进来
     *
     * @param
     */
    public static void updateAndAddTank(FCTank tank) {
        if (tank != null) {
            FCTank find = findTankByName(tank.getHostId(), tank.getID());
            if (find == null){
                mutilTanks.add(tank);
            }else {
                find.setChassisID(tank.getChassisID());
                find.setGunID(tank.getGunID());
                find.setGun2ID(tank.getGun2ID());
                find.setSEID(tank.getSEID());
                find.setCdeviceID(tank.getCdeviceID());
                find.setRadarID(tank.getRadarID());
                find.setEngineID(tank.getEngineID());
                find.setKnapsack(tank.getKnapsack());
                find.setPmapname(tank.getPmapname());
                find.setAcidState(tank.isAcidState());
                find.setResidues(tank.getResidues());
                find.setDefense(tank.getDefense());
            }
            tank.reInitShuxing();
            System.out.println("updateAndAddTank "+tank.hashCode());

        }
    }

    /**
     * 一次性删除对应hostid的坦克
     * @param hostId
     */
    public static void delAllTanksByHostId(String hostId) {
        System.out.println("delAllTanksByHostId "+hostId);
        ArrayList<FCTank> deltanks = new ArrayList<>(0);
        for (FCTank t : mutilTanks) {
            if (t.getHostId().equals(hostId)) {
                deltanks.add(t);
            }
        }
        mutilTanks.removeAll(deltanks);
    }


}
